//
//  CCCommentViewModel.m
//  Activity
//
//  Created by its on 15/5/21.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCCommentViewModel.h"
#import <AVOSCloud/AVOSCloud.h>
#import "CCError.h"
#import <LinqToObjectiveC/NSArray+LinqExtensions.h>
#import "CCCommentModel.h"
#import "CCComCellViewModel.h"

@interface CCCommentViewModel ()
@property (nonatomic, strong) CCCommentModel *model;
@end

@implementation CCCommentViewModel
- (id) initWithModel:(CCCommentModel *) model {
  if ( self = [super init]) {
    _model = model;
    _listArray = [NSMutableArray array];
    _myCommentList = [NSArray array];
  }
  return self;
}

- (RACSignal *) setViewModelActivity {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    RACDisposable *disposable =  [[_model getCommentList:10 activity:_activityid] subscribeNext:^(id x) {
      if ([x isKindOfClass:[CCError class]]) {
        [subscriber sendNext: x];
      } else {
        NSArray *array = [x linq_select:^id(CCComment *item) {
          return [[CCComCellViewModel alloc] initWithComment:item commentBlock:^(NSString *id, NSString *name, NSString *author) {
            self.replyid = id;
            self.replyName = name;
            self.replyAuthor = author;
            self.placehold = self.replyName;
          }];
        }];
        [_listArray addObjectsFromArray: array];
        _lastid = [(CCComment *)[x lastObject] objectid];
        if (array.count != 0) {
          [subscriber sendNext:ERROR(3002)];
        } else {
          [subscriber sendNext: ERROR(3004)];
        }
        [subscriber sendCompleted];
      }
    }];
    RACDisposable *dis = [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];
    RACCompoundDisposable *compound = [RACCompoundDisposable compoundDisposable];
    [compound addDisposable: disposable];
    [compound addDisposable:dis];
    return compound;
  }];
}

- (RACSignal *) getFoot {
  if (_lastid == nil) {
    return [self setViewModelActivity];
  } else {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
      RACDisposable *disposable =  [[_model getCommentList:10 activity:_activityid after:_lastid] subscribeNext:^(id x) {
        if ([x isKindOfClass:[CCError class]]) {
          [subscriber sendNext: x];
          [subscriber sendCompleted];
        } else if([(NSArray *)x count] == 0) {
          [subscriber sendNext:ERROR(3006)];
          [subscriber sendCompleted];
        } else {
            _lastid = [(CCComment *)[x lastObject] objectid];
          NSArray *array = [x linq_select:^id(CCComment *item) {
            return [[CCComCellViewModel alloc] initWithComment:item commentBlock:^(NSString *id, NSString *name, NSString *author) {
              self.replyid = id;
              self.replyName = name;
              self.replyAuthor = author;
              self.placehold = self.replyName;
            }];
          }];
            [_listArray addObjectsFromArray: array];
            [subscriber sendNext:ERROR(3002)];
            [subscriber sendCompleted];
          }
      }];
      RACCompoundDisposable *compound = [RACCompoundDisposable compoundDisposable];
      [compound addDisposable: disposable];
      RACDisposable *dis = [RACDisposable disposableWithBlock:^{
        [subscriber sendCompleted];
      }];
      [compound addDisposable: dis];
      return compound;
    }];    
  }
}

- (RACSignal *) publicComment {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    if ( ![AVUser currentUser]) {
      [subscriber sendNext: @{@"error":ERROR(4001),@"objectid":[NSNull null]}];
      [subscriber sendCompleted];
      // please go to login
    } else if ( ![[AVUser currentUser] isAuthenticated] ) {
      [subscriber sendNext: @{@"error":ERROR(4002),@"objectid":[NSNull null]}];
      [subscriber sendCompleted];
      // please to author
    } else if (!self.content || self.content.length == 0) {
      [subscriber sendNext: @{@"error":ERROR(3005),@"objectid":[NSNull null]}];
      [subscriber sendCompleted];
    }
    else {
      CCPublicComment *comment = [[CCPublicComment alloc] init];
      comment.author = [AVUser currentUser];
      if (self.replyid) {
        comment.parent = [AVObject objectWithoutDataWithClassName:@"comment" objectId:self.replyid];
      }
      
      comment.activity = self.activityid;
      comment.content = self.content;
      comment.upArray = @[];
      comment.upCount = 0;
      [[self.model publicComment:comment] subscribeNext:^(NSDictionary *x) {
        if ([(CCError *)x[@"error"] code] == 3000) {
          [self createCommentCell: x[@"objectid"]];
          self.content = nil;          
        }
        [subscriber sendNext: x];
        [subscriber sendCompleted];
      }];
    }
    return [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];
  }];
}

- (RACSignal *) reply {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [[self publicComment] subscribeNext:^(NSDictionary *x) {
      if ([(CCError *)x[@"error"] code] == 3000) {
        [[self.model publicPrivateMessage:@{@"text":@"回复了你",
                                            @"author":[[AVUser currentUser] objectForKey:@"username"],
                                            @"activityTitle":self.activityTitle,
                                            @"activityId":self.activityid,
                                            @"replyId":x[@"objectid"]
                                            }
                                userid:self.replyAuthor] subscribeNext:^(id x) {
          
        }];
        self.replyid = self.replyAuthor = self.replyName = nil;
        [subscriber sendNext: x];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      
    }];
  }];
}


#pragma mark private

- (void) createCommentCell:(NSString *) objectid {
  CCComment *publicComment = [[CCComment alloc] init];
  publicComment.author = [[AVUser currentUser] objectForKey:@"username"];
  publicComment.authorid = [[AVUser currentUser] objectId];
  publicComment.head = [[AVUser currentUser] objectForKey:@"headImage"];
  publicComment.comment = self.content;
  publicComment.commentAuthorId = self.replyid;
  publicComment.commentAuthor = self.replyName;
  publicComment.upCount = @(0);
  publicComment.upid = @[];
  publicComment.objectid = objectid;
  CCComCellViewModel *viewmodel = [[CCComCellViewModel alloc] initWithComment: publicComment commentBlock:^(NSString *id, NSString *name, NSString *author) {
    self.replyid = id;
    self.replyName = name;
    self.replyAuthor = author;
    self.placehold = self.replyName;
  }];
  self.myCommentList = [self.myCommentList arrayByAddingObject: viewmodel];
}

- (void) sendCommentMessage:(NSString *) commentId {
  [[self.model publicCommentMessage:@{@"text":@"回复了你",
                                      @"author":[[AVUser currentUser] objectForKey:@"username"],
                                      @"activityTitle":self.activityTitle,
                                      @"activityId":self.activityid,
                                      @"replyId":commentId
                                      }
                             userid:self.activityAuthor] subscribeNext:^(id x) {
    
  }];
}

- (void) dealloc {
  
}

@end





